package benchmark;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * hbase压力测试代码，生产中，需要用多线程来测试
 */
public class HbaseBenchtest {

    public static void main(String[] args) throws IOException {

        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","master3:2181,master4:2181,master5:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table htable = conn.getTable(TableName.valueOf("yiniu"));
        Long   start=System.currentTimeMillis();
        for (int i = 0; i <1000; i++) {
            Get get = new Get(Bytes.toBytes(StringUtils.leftPad(RandomUtils.nextInt(0, 100000) + "", 6, "0")));
            String q1=RandomUtils.nextInt(1,1000)+"k";
            String q2=RandomUtils.nextInt(1,1000)+"k";
            String q3=RandomUtils.nextInt(1,1000)+"k";
            get.addColumn(Bytes.toBytes("f")   ,Bytes.toBytes(q1));
            get.addColumn(Bytes.toBytes("f")   ,Bytes.toBytes(q2));
            get.addColumn(Bytes.toBytes("f")   ,Bytes.toBytes(q3));

            Result result = htable.get(get);
            result.getValue(Bytes.toBytes("f"),Bytes.toBytes(q1));
            result.getValue(Bytes.toBytes("f"),Bytes.toBytes(q2));
            byte[] fs = result.getValue(Bytes.toBytes("f"), Bytes.toBytes(q3));
            String s = new String(fs);
            System.out.println(s);
        }
        Long   end=System.currentTimeMillis();
        System.out.println(end-start);  //结果4666

    }

}
